package Frame.shopSet;

import FCRendering.sw.FCRValues;
import Frame.entity.UnifyShopSayUIEntity;
import Function.FCMutilPlayCache;
import VWorldGameStudio.FCPlayerInfo;
import lombok.Setter;
import socket.FCRSocketClient;
import socket.FCRSocketServer;

import java.util.ArrayList;
import java.util.function.BiConsumer;

import static FCRendering.FCRMaster.fcv;
import static FCRendering.FCRMaster.gameSave;

public class FunctionOpenMMVVIP extends AShopFuntion {

    private ArrayList<FCPlayerInfo> serviceList = new ArrayList<>();

    private FCPlayerInfo selectP;
    private int showFristIndex = 0;

    @Setter
    private int endI = 0;


    public void reBuildWindow() {
        selectP = null;
        endI = 0;
        showFristIndex = 0;
        serviceList.clear();
        FCMutilPlayCache.gamePlayers.forEach(new BiConsumer<String, FCPlayerInfo>() {
            @Override
            public void accept(String s, FCPlayerInfo playerInfo) {
                if (!playerInfo.getAliasUID().equals(gameSave.HOST_UID) && !playerInfo.isANpcPlayer()) {
                    serviceList.add(playerInfo);
                }
            }
        });

        setEndI(Math.min(serviceList.size(), showFristIndex + 6));
        resetUI();
    }

    private void resetUI() {
        FCRValues.resetUnifySayUIEntity();
        int m = 0;
        for (int i = showFristIndex; i < endI; i++) {
            FCRValues.leftWindowUI[m].leftText = "成员:" + serviceList.get(i).getChName();
            FCRValues.leftWindowUI[m].rightText = "编码:" + serviceList.get(i).getAliasUID();
            m++;
        }
        if (selectP != null) {
            FCRValues.rightWindowUI[0].rightText = "选择了" + selectP.getAliasUID() + ":" + selectP.getChName();
        }
        FCRValues.rightWindowUI[1].rightText = "上一页";
        FCRValues.rightWindowUI[2].rightText = "下一页";
        FCRValues.rightWindowUI[3].rightText = "邀请组队";
        FCRValues.rightWindowUI[4].rightText = "申请进队";
        FCRValues.rightWindowUI[5].rightText = "退出组队";
    }


    @Override
    public void clickLeftWindow(int i, UnifyShopSayUIEntity entity) {
        if (entity.leftText.length() > 1) {
            selectP = serviceList.get(i);
            resetUI();
        }
    }

    @Override
    public void clickRightWindow(int i, UnifyShopSayUIEntity entity) {
        if (i == 5) {
            if (!fcv.isShowTeamRequest()) {
                if (fcv.isSever()) {
                    FCRSocketServer.getIn().playerJoinOut();
                } else if (fcv.isClient()) {
                    FCRSocketClient.getIn().playerJoinOut();
                }
            }
            fcv.setOpenMMWVIP(false);
        } else if (i == 1) {
            if (showFristIndex >= 6) {
                showFristIndex -= 6;
                selectP = null;
                setEndI(Math.min(serviceList.size(), showFristIndex + 6));
                resetUI();
            }
        } else if (i == 2) {
            if (endI < serviceList.size()) {
                showFristIndex += 6;
                selectP = null;
                setEndI(Math.min(serviceList.size(), showFristIndex + 6));
                resetUI();
            }
        } else if (i == 3 && selectP != null) {
            if (fcv.isLockClickPlayerHost()) {
                fcv.getMessage().add("请等待前一个申请组队的答复！");
                fcv.showMessage();
            } else {
                if (!fcv.isShowTeamRequest()) {
                    fcv.setCurTeamMenuClickPlayerHost(selectP.getAliasUID());
                    if (fcv.isSever()) {
                        FCRSocketServer.getIn().requestJoinMe();
                    } else if (fcv.isClient()) {
                        FCRSocketClient.getIn().requestJoinMe();
                    }
                    fcv.setOpenMMWVIP(false);
                } else {
                    fcv.getMessage().add("请先处理完当前组队请求！");
                    fcv.showMessage();
                }
            }
        } else if (i == 4 && selectP != null) {
            if (fcv.isLockClickPlayerHost()) {
                fcv.getMessage().add("请等待前一个申请组队的答复！");
                fcv.showMessage();
            } else {
                if (!fcv.isShowTeamRequest()) {
                    fcv.setCurTeamMenuClickPlayerHost(selectP.getAliasUID());
                    if (fcv.isSever()) {
                        FCRSocketServer.getIn().requestJoinYou();
                    } else if (fcv.isClient()) {
                        FCRSocketClient.getIn().requestJoinYou();
                    }
                    fcv.setOpenMMWVIP(false);
                } else {
                    fcv.getMessage().add("请先处理完当前组队请求！");
                    fcv.showMessage();
                }
            }
        }
    }


}
